<?php
/**
* Hellos Model for Hello World Component
*
* @package Joomla.Tutorials
* @subpackage Components
* @link http://dev.joomla.org/component/option,com_jdwiki/
Itemid,31/id,tutorials:components/
* @license GNU/GPL
*/
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();
jimport( 'joomla.application.component.model' );
/**
* Hello Model
*
* @package Joomla.Tutorials
* @subpackage Components
*/
class RulesModelRules {
	/**
	*
	* @var array
	*/
	
	
	/**
	* Method Construction
	*/
	function __construct() {
	}
	

	/**
	* Method get Id of Section (only one) which user or group has permission
	* $scope: must be value have sign "'', mean: "'content'"
	* @return Querry String
	*/
	
	function getQuerySection_Rule($scope ="'content'", $order = 's.ordering', $id = 'AS value', $title = 'AS text'){
		$query = 'SELECT DISTINCT s.id '.$id.', s.title '.$title.
				 ' FROM #__sections AS s'.
				 ' INNER JOIN #__categories AS cc ON cc.section = s.id' .
				 ' WHERE s.published = 1'.
				 ' AND s.scope = ' . $scope.
				 ' AND cc.id IN '.
				   $this->getQueryCategory_Rule().
				 ' ORDER BY ' . $order; 
		return $query;
	}
	
	/**
	* Method get Id of Categoris (only one) which user or group has permission
	* @return Querry String
	*/
	
	function getQueryCategory_Rule() {
		//BEGIN: TUYEN - GET Id of Categoris (only one) which user or group has permission
		$user			= & JFactory::getUser();
		
		//Query all from Content & Category in Rules Table								
		$queryCategory_Rule=' (SELECT DISTINCT cat.id'.
							' FROM #__categories AS cat';
		if ( $user->get('gid') != 25) {//Not be a Supper Administrator -> will check rules	
			//Query get all of Categories in Rules Table
			$queryCategorInRule = ' (SELECT DISTINCT cc.id'.
								  ' FROM #__categories AS cc'.
								  ' RIGHT JOIN #__access_rules AS ru ON ru.idContentType=cc.id'.
								  ' WHERE ru.contentType = "CATEGORY"'.
								  ' AND ('.
								  ' ((ru.idUserType = '.(int)$user->get('id').') AND (ru.userType = "USER"))'.
								  ' 	OR'.
								  ' ((ru.idUserType = '.(int)$user->get('gid').') AND (ru.userType = "GROUP"))'.
								  ' ))';//END - AND; END - SELECT
	
			//Query get all of Categories which have content in Rules Table		
			$queryCategorOfContentInRule = ' '.
								' (SELECT DISTINCT c.catid'.
								' FROM #__content AS c'.	
								' RIGHT JOIN #__access_rules AS r ON r.idContentType = c.id' .
								' WHERE r.contentType = "CONTENT"'.
								' AND ('.
								' ((r.idUserType = '.(int)$user->get('id').') AND (r.userType = "USER"))'.
								' 	OR'.
								' ((r.idUserType = '.(int)$user->get('gid').') AND (r.userType = "GROUP"))'.
								' ))';//END - AND; END - SELECT

			$queryCategory_Rule = $queryCategory_Rule.' WHERE (cat.id IN '.$queryCategorInRule.') OR (cat.id IN '.$queryCategorOfContentInRule.'))';
		} else {//Be a Supper Administrator -> having all of rules
			$queryCategory_Rule = $queryCategory_Rule.')';
		}
		//END: TUYEN - GET Id of Categoris (only one) which user or group has permission
		return $queryCategory_Rule;
	}
	
	/**
	* Method get Rules of User
	*
	* @idUssr
	* $idContentType: Id of Content Type
	* $contentType = Content, category, Section or Articles
	* @return Rules
	*/
	function getRules($idUser, $idContentType = null, $contentType = null, $idGroupOfUser = null) {
		try {
			echo ($idGroupOfUser);
			if ($idGroupOfUser == 25) {//Supper Administrator
				return $this->setRules("DELETE EDIT PUBLIC");//means: having all of rules
			}
			
			//Processing IF not be a Supper Administrator
			//Check User
			$db				= & JFactory::getDBO();		
			$__query = 'SELECT id, rules' .
						' FROM #__access_rules'.
						' WHERE idContentType = '. (int) $idContentType.
						' AND contentType = "'.$contentType.'"'.
						' AND idUserType = '. (int) $idUser.
						' AND userType = "USER"';
			$db->setQuery($__query);
			//Get only object
			$user_rules = $db->loadObject();
		
			if (count ($user_rules) == 0) {	//Begin if: Check USER of Article							
				//If has no User -> check Group						
				$__query = 'SELECT id, rules' .
						' FROM #__access_rules'.
						' WHERE idContentType = '. (int) $idContentType.
						' AND contentType = "'.$contentType.'"'.
						' AND idUserType = '. (int) $idGroupOfUser.
						' AND userType = "GROUP"';

				$db->setQuery($__query);
				//Get only objectsd
				$group_rules = $db->loadObject();
				if (count ($group_rules) == 0) {//Begin if: Check GROUP of Article		
					//Check Rule of Category of Id Article
					$__query = 'SELECT catid' .
						' FROM #__content'.
						' WHERE id = '. (int) $idContentType;
					$db->setQuery($__query);					
					$__content = $db->loadObject();
					
					$idContentType = $__content->catid;

					//Check Rule User to this Category
					$__query = 'SELECT id, rules' .
						' FROM #__access_rules'.
						' WHERE idContentType = '. (int) $idContentType.
						' AND contentType = "CATEGORY"'.
						' AND idUserType = '. (int) $idUser.
						' AND userType = "USER"';
					$db->setQuery($__query);
					
					//Get only object
					$user_rules = $db->loadObject();
					if (count ($user_rules) == 0) {	//Begin if: Check USER of Category
						//If has no User -> check Group						
						$__query = 'SELECT id, rules' .
							' FROM #__access_rules'.
							' WHERE idContentType = '. (int) $idContentType.
							' AND contentType = "CATEGORY"'.
							' AND idUserType = '. (int) $idGroupOfUser.
							' AND userType = "GROUP"';

						$db->setQuery($__query);
						//Get only objectsd
						$group_rules = $db->loadObject();
						if (count ($group_rules) == 0) {//Check GROUP of Category		
							return $this->setRules ("");//Has no rule
						} else {//Check Rule of Group
							return $this->setRules($group_rules->rules);
						}
						
					} else {
						return $this->setRules($user_rules->rules);	
					} //End if: Check USER of Category
							
					
				} else {//Check Rule of Group
					return $this->setRules($group_rules->rules);
				} //End if: Check GROUP of Article		
			} else {
				return $this->setRules($user_rules->rules);
			}//End if: Check USER of Article	
			
		} catch (Exception $e) {
			$this->setError($e->getMessage());
			return false;
		}

	}
	
	/**
	* Method Set Rules of User
	* EDIT: true or false
	* DELETE: true or false
	* PUBLIC: true or false	
	*/	
	function setRules($__rules) {
		//SETUP BEGIN any User has no Rule
		$rulesObject->no_rule = true;
		$rulesObject->edit_rule = false;
		$rulesObject->delete_rule = false;

		$__rules = strtoupper($__rules);
		//Check EDIT RULE
		if (substr_count($__rules, "EDIT") > 0) {			
			$rulesObject->edit_rule = true;
			$rulesObject->no_rule = false;
		} 
		
		//Check DELETE RULE
		if (substr_count($__rules, "DELETE") > 0) {			
			$rulesObject->delete_rule = true;
			$rulesObject->no_rule = false;			
		} 
		
		//Check PUBLIC RULE
		if (substr_count($__rules, "PUBLIC") > 0) {			
			$rulesObject->public_rule = true;
			$rulesObject->no_rule = false;			
		}
		return $rulesObject;
	}
	
}